package com.organization

import grails.test.*
import grails.converters.JSON

class TreeControllerTests extends GrailsUnitTestCase {

    TreeController controller
    TreeService treeService
    CommonService commonService

    protected void setUp() {
        super.setUp()
        //create and save data objects
        def dep1 = new Department(name: "Dep1")
        def dep2 = new Department(name: "Dep2")
        def proj1 = new Project(department: dep2, name: "Proj1", startDate: new Date())
        def proj2 = new Project(department: dep2, name: "Proj2", startDate: new Date())
        def proj3 = new Project(department: dep2, name: "Proj3", startDate: new Date())
        def empl1 = new Employee(project: proj2, lastName: "Ivanov", firstName: "Ivan")
        def empl2 = new Employee(project: proj2, lastName: "Petrov", firstName: "Ivan")
        def empl3 = new Employee(project: proj2, lastName: "Sidorov", firstName: "Ivan")
        def empl4 = new Employee(project: proj1, lastName: "Stepanov", firstName: "Ivan")
        assertNotNull "can't save department",dep1.save()
        assertNotNull "can't save department",dep2.save()
        assertNotNull "can't save project",proj1.save()
        assertNotNull "can't save project",proj2.save()
        assertNotNull "can't save project",proj3.save()
        assertNotNull "can't save employee",empl1.save()
        assertNotNull "can't save employee",empl2.save()
        assertNotNull "can't save employee",empl3.save()
        assertNotNull "can't save employee",empl4.save()
        //init controller
        controller = new TreeController()
        controller.treeService = treeService
    }

    protected void tearDown() {
        super.tearDown()
    }

    //tests for getBranch method, used in treeController
    void testGetBranchForNull() {
        controller.index()
        //parse the JSON
        def controllerResponse = controller.response.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertEquals 0, jsonResult.size()
    }

    void testGetBranchForEmployee() {
        def empl = Employee.findByLastName("Sidorov")
        assertNotNull "can't find employee",empl

        //fill params
        controller.params.nodeId = "Employee_"+empl.id
        //call action
        controller.index()
        //parse the JSON
        def controllerResponse = controller.response.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertEquals 0, jsonResult.size()
    }

    void testGetBranchForProject() {
        def proj = Project.findByName("Proj2")
        assertNotNull "can't find project",proj

        //fill params
        controller.params.node = "Project_"+proj.id
        //call action
        controller.index()
        //parse the JSON
        def controllerResponse = controller.response.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertEquals 3, jsonResult.size()
        assertEquals "Sidorov Ivan",jsonResult[2].text
        assertEquals "true",jsonResult[2].leaf

        def empl = Employee.findByLastName("Sidorov")
        assertNotNull "can't find employee",empl.id
        assertEquals "Employee_"+empl.id,jsonResult[2].id
    }

    void testGetBranchForDepartment() {
        def dep = Department.findByName("Dep2")
        assertNotNull "can't find department",dep

        //fill params
        controller.params.node = "Department_"+dep.id
        //call action
        controller.index()
        //parse the JSON
        def controllerResponse = controller.response.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertEquals 3, jsonResult.size()
        assertEquals "Proj2",jsonResult[1].text
        assertEquals 3,jsonResult[1].children.size()

        def proj = Project.findByName("Proj2")
        assertNotNull "can't find project",proj.id
        assertEquals "Project_"+proj.id, jsonResult[1].id
        assertEquals "Proj2", jsonResult[1].text
        assertEquals 3, jsonResult[1].children.size()

        def empl = Employee.findByLastName("Ivanov")
        assertNotNull "can't find employee",empl.id
        assertEquals "Employee_"+empl.id, jsonResult[1].children[0].id
        assertEquals "Ivanov Ivan", jsonResult[1].children[0].text
        assertEquals "true", jsonResult[1].children[0].leaf
    }

    void testGetBranchForRoot() {
        //fill params
        controller.params.node = commonService.rootNodeStr
        //call action
        controller.index()
        //parse the JSON
        def controllerResponse = controller.response.contentAsString
        def jsonResult = JSON.parse(controllerResponse)
        //navigate the JSON as an object
        assertEquals 2, jsonResult.size()
        assertEquals "Dep1",jsonResult[0].text
        assertEquals 0,jsonResult[0].children.size()
        assertEquals "Dep2",jsonResult[1].text
        assertEquals 3,jsonResult[1].children.size()

        def proj = Project.findByName("Proj2")
        assertNotNull "can't find project",proj.id
        assertEquals "Project_"+proj.id, jsonResult[1].children[1].id
        assertEquals "Proj2", jsonResult[1].children[1].text
        assertEquals 3, jsonResult[1].children.size()

        def empl = Employee.findByLastName("Ivanov")
        assertNotNull "can't find employee",empl.id
        assertEquals "Employee_"+empl.id, jsonResult[1].children[1].children[0].id
        assertEquals "Ivanov Ivan", jsonResult[1].children[1].children[0].text
        assertEquals "true", jsonResult[1].children[1].children[0].leaf
    }




}
